<?php
namespace Module\Manage;

use Model\Bug as bugModel;
use Model\Model;

/**
 * bug管理
 * @author shooke
 *
 */
class Bug extends Init
{
    /**
     * bug统计
     */
    public function index()
    {
        $model = new bugModel();
        //错误级别统计
        $sql = "select count(*) as count,level from {pre}bug group by level";
        $level = $model->query($sql);
        if (is_array($level)){
            foreach ($level as $val){
                $level['count'][] = $val['count'];
                $level['level'][] = $val['level']."级";
            }
        }
        
        //项目统计
        $sql = "select count(*) as count,project from {pre}bug group by project";
        $project = $model->query($sql);
        if (is_array($project)){
            foreach ($project as $val){
                $project['count'][] = $val['count'];
                $project['project'][] = $val['project'];
            }
        }
        
        //发现人统计        
        $sql = "select count(*) as count,finder from {pre}bug group by finder";
        $finder = $model->query($sql);
        if(is_array($finder)){
            foreach ($finder as $val){
                $finder['count'][] = $val['count'];
                $finder['finder'][] = $val['finder'];
            }
        }     
           
        $this->assign(['level'=>$level,'project'=>$project,'finder'=>$finder]);
        $this->display();
    
    }
    /**
     * bug列表和筛选
     * @param unknown $r
     */
    public function record($r)
    {
        $finder = bugModel::field('finder')->group('finder')->fetchAll();
        $level = bugModel::field('level')->group('level')->fetchAll();
        $project = bugModel::field('project')->group('project')->fetchAll();
        $where = ['delete'=>0,'priority'=>''];
        $r->get('level') && $where['level'] = $r->get('level');
        $r->get('finder') && $where['finder'] = $r->get('finder');
        $r->get('project') && $where['project'] = $r->get('project');
                   
        $bug = bugModel::where($where)->page(['rows'=>10,'wrap'=>'div'])->order('id desc')->fetchAll();
       
        
        $pagestring = bugModel::pageString();
        $this->assign(['bug'=>$bug,
            'pagestring'=>$pagestring,
            'finder'=>$finder,
            'level'=>$level,
            'project'=>$project
        ]);
        
        $this->display();
    }
    /**
     * bug详情
     * @param unknown $r
     */
    public function detail($r)
    {
       
        $id = $r->param('id');
        
        $where = ['id'=>$id];
        
        $bug = bugModel::where($where)->fetch();
        
        $this->assign($bug);
        $this->display();
    }
    /**
     * bug添加界面
     */
    public function add()
    {
        $this->display();
    }
    /**
     * bug添加操作
     */
    public function doAdd()
    {
        $model = new bugModel();
        $model->validate();
        $error = $model->getError();
        if($error){
            $this->error($error[0]);
        }
        $model->insert();
        $this->success('提交成功');
    }
    /**
     * 删除数据
     * @param unknown $r
     */
    public function delete($r)
    {
        $id = $r->param('id');
        $where = ['id'=>$id];
        $model = new bugModel();
        $model->where($where)->data(['delete'=>1])->update();
        $this->success('删除成功');
    }
    
    /**
     * 修改页面
     * @param unknown $r
     */
    public function edit($r)
    {
        $id = $r->param('id');
        $where = ['id'=>$id];       
        $bug = bugModel::where($where)->fetch();
        $this->assign($bug);
        $this->display();
    }
    /**
     * 保存修改操作
     */
    public function doEdit($r)
    {
        $id = $r->post('id');
        $where = ['id'=>$id];
        $model = new bugModel();
        $model->validate();
        $error = $model->getError();
        if($error){
            $this->error($error[0]);
        }
        $model->where($where)->update();
        $this->success('修改成功');
    }
    public function doWork($r)
    {
        $id = $r->post('id');
        $where = ['id'=>$id];
        $model = new bugModel();
        $data=$r->post();
        $model->where($where)->data($data)->update();
        $bug = $model->where('id<'.$id)->order("id desc")->fetch();
        $this->success('处理成功','Bug/record');
    }
}